﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace ConsoleApplication1
{
    public static class Extensions
    {
        //以IEnumerable<T>实现List<T>的ForAll()
        public static void ForAll<T>(this IEnumerable<T> sequence, Action<T> action)
        {
            foreach (T item in sequence)
            {
                action(item);
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            TestDemo ts = new TestDemo();
            ts.Name = "张三";
            ts.Pwd = "123";
            //调用重写的ToString()方法
            Console.WriteLine(ts.ToString());


            //填充一个数组，并将其输出
            int[] foo = (from n in Enumerable.Range(0, 100)
                         select n * n).ToArray();
            foo.ForAll((n) => Console.WriteLine());



            //WeatherDataStreram 可以作为Linq查询的数据源
            var warmDays = from item in new WeatherDataStreram("Ann Arbor")
                           where item.Temperature > 80
                           select item;






            #region MyRegion
            Console.ReadKey(); 
            #endregion
        }

        //使用查询语法 处理嵌套循环，生成（0，99）整数的所有（X，Y）的二元组
        //Tuple<T1,T2>二元组
        private static IEnumerable<Tuple<int, int>> QueryIndices()
        {
            return from x in Enumerable.Range(0, 100)
                   from y in Enumerable.Range(0, 100)
                   //二元组(X,Y)中X、Y的和小于100
                   where x + y < 100
                   //让返回的二元组按照其远离点的距离 逆序排序
                   orderby (x * x + y * y) descending
                   select Tuple.Create(x, y);


            //var storge = new List<Tuple<int, int>>();
            //for (int i = 0; i < 100; i++)
            //{
            //    for (int j = 0; j < 100; j++)
            //    {
            //        //yield,迭代标识符； 
            //        //yield return 返回下一个迭代; yield break;结束迭代
            //        if (i + j < 100)
            //            //yield return Tuple.Create(i, j);
            //            storge.Add(Tuple.Create(i, j));
            //    }
            //}
            ////命令式版本的查询赋值不好理解
            //storge.Sort((point1, point2) => 
            //    (point2.Item1 * point1.Item2 + point2.Item2 * point1.Item1).CompareTo
            //    (point1.Item1 * point1.Item1 + point2.Item2 * point2.Item2));
            //return storge;
        }


        //支持IEnumerable<T>的接口类型都可以使用该方法
        public static void PaintCollection<T>(IEnumerable<T> collection)
        {
            foreach (T item in collection)
            {
                Console.WriteLine("Collection contains {0}",item.ToString());
            }
        }


    }


}
